//
//  XGCategoryButton.m
//  新闻
//
//  Created by 小果 on 16/3/30.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGCategoryButton.h"
#import "NSString+Dir.h"
@interface XGCategoryButton()
{
    UIImageView         *_deleteImageView;      // 小叉叉图像视图
    UIPanGestureRecognizer  *_panGesture;       // 拖拽手势
}

@end

@implementation XGCategoryButton

- (id)initWithFrame:(CGRect)frame title:(NSString *)title index:(NSInteger)index
{
    self = [super initWithFrame:frame];
    
    if (self) {
        // 标题
        [self setTitle:title forState:UIControlStateNormal];
        
        // 索引
        _index = index;
        
        // 颜色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setBackgroundColor:[UIColor greenColor]];
        
        // 字体
        self.titleLabel.font = kNCNormalFont;
    }
    
    return self;
}

#pragma mark 设置按钮类型，针对不同的类型，改变按钮的外观
- (void)setType:(kNCButtonType)type
{
    _type = type;
    
    if (_panGesture != nil) {
        [self removeGestureRecognizer:_panGesture];
    }
    
    if (_type == kNCButtonTypeNormal) {
        // 无背景
        [self setBackgroundImage:nil forState:UIControlStateNormal];
        
        // 删除小叉叉视图
        if (_deleteImageView != nil) {
            // 只是从界面上删除了_deleteImageView，成员变量本身并不会收到影响
            [_deleteImageView removeFromSuperview];
        }
    } else {
        // 有背景
        NSString *bgPath = kNCImagePath(@"channel_button_bg@2x.png");
        UIImage *bgImage = [UIImage imageWithContentsOfFile:bgPath];
        
        [self setBackgroundImage:bgImage forState:UIControlStateNormal];
        
        if (_type == kNCButtonTypeMoveable) {
            if (_deleteImageView == nil) {
                // 增加图示图片，左上角的小叉叉
                NSString *path = kNCImagePath(@"channel_edit_delete@2x.png");
                UIImage *image = [UIImage imageWithContentsOfFile:path];
                
                _deleteImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
                _deleteImageView.image = image;
                _deleteImageView.center = CGPointMake(3.0, 3.0);
            }
            
            // 将小叉叉添加到按钮中
            [self addSubview:_deleteImageView];
            
            // 添加按钮的手势监听
            if (_panGesture == nil) {
                _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
            }
            
            [self addGestureRecognizer:_panGesture];
        }
    }
}

#pragma mark - 手势拖动
- (void)panGesture:(UIPanGestureRecognizer *)recognizer
{
    // 1. 取出点
    CGPoint location = [recognizer translationInView:self];
    
    if (UIGestureRecognizerStateChanged == recognizer.state) {
        // 修改按钮的平移形变
        self.transform = CGAffineTransformTranslate(self.transform, location.x, location.y);
        
        [recognizer setTranslation:CGPointZero inView:self];
        
        [_delegate categoryButtonIsMoving:self];
    } else if (UIGestureRecognizerStateEnded == recognizer.state) {
        [_delegate categoryButtonDidMoved:self];
    }
}
@end
